package com.gdkm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdkm.po.Doctor;
import com.gdkm.po.Medical;
import com.gdkm.po.Student;
import com.gdkm.po.User;
import com.gdkm.po.UserManagement;
import com.gdkm.service.MedicalService;
import com.gdkm.service.UserService;

@Controller
public class FirstController {
	@Autowired

	UserService service;
	@Autowired
	MedicalService service1;

	@RequestMapping(value = "/login1", method = RequestMethod.GET)
	public String login2() {
		return "login1";

	}

	
	@RequestMapping(value = "/Pe", method = RequestMethod.GET)
	public String Pe() {
		return "Pe";

	}

	@RequestMapping(value = "/login1", method = RequestMethod.POST)
	public String login1() {
		return "login1";

	}

	@GetMapping("/uregister1")
	public String save(String usercode, String password, String name,HttpServletRequest request) {
		List<Student> list = service.list8(usercode);
		if(list.size()==0) {
			service.save2(usercode, password,name);	
		}
		
		return "login1";
	}
	
	@ResponseBody
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	public Object chekUser(String usercode) {
		Student student = service.findUsercode(usercode);
		if (!(usercode == null) && student == null) {
			return "ok";
		} else {
			return "已经注册";
		}
	}
	
	@RequestMapping(value = "/ulogin1", method = RequestMethod.POST)

	public String denglu(Student user, HttpSession session, HttpServletRequest request) {

		String usercode = user.getUsercode();
		String password = user.getPassword();

		try {
			Student sqluser = service.get1(usercode);
			String sqlpassword = sqluser.getPassword();
			String m_name = sqluser.getName();
			String k = sqluser.getUsercode();
			System.out.println("日历" + m_name);
			if (usercode != null && password != null && password.equals(sqlpassword)) {
				session.setAttribute("User_session", user);
				session.setAttribute("person", k);
				session.setAttribute("personName", m_name);
				return "redirect:y";
			}

		} catch (NullPointerException e) {
			System.out.println("账号不存在");
		}
		return "login1";
	}

	@RequestMapping("/Pe")
	public String ss() {
		return "Pe";

	}

	@RequestMapping(value = "/zhuye1")
	@ResponseBody
	public List zhuye1(HttpSession session) {

		session.getAttribute("User_session");
		String usercode = (String) session.getAttribute("User_session1");
		System.out.println("哈" + usercode);
		List<Student> list = service.list8(usercode);

		System.out.println("11112" + service);
		System.out.println("草泥马1" + usercode);
		return list;

	}


	@RequestMapping("/update8")
	public String update(Student stu) {
		service.update(stu);
		System.out.print("牛皮" + stu);
		return "xiugai";
	}

	@RequestMapping("/bingli")
	public String sl() {
		return "bingli";

	}

	@ResponseBody
	@RequestMapping("/list10")
	public List get(HttpSession session) {
		String m_name = (String) session.getAttribute("personName");
		System.out.println("999" + m_name);
		List<Medical> list2 = service1.list4(m_name);
		System.out.println(list2.toString());
		return list2;
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		 return "redirect:login1";
		
	}
}
